package com.ysx.article.listener;

import com.alibaba.fastjson.JSON;
import com.ysx.article.service.ApArticleConfigService;
import com.ysx.common.constants.BusinessConstants;
import com.ysx.common.pojo.Result;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class SyncDownOrUpArticleListener {

    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @KafkaListener(topics = BusinessConstants.MqConstants.WM_NEWS_DOWN_OR_UP_ADMIN_MQTOPIC)
    public Result<?> syncDownOrUpArticleListener(ConsumerRecord<?,?> record)  {
        Map map = JSON.parseObject(String.valueOf(record.value()), Map.class);
        String type = (String) map.get("type");
        String articleId = (String) map.get("articleId");
        //判断是否为null
        if (type == null || articleId == null) {
            return Result.ok("文章或者下架信息为空");
        } else {
            Integer isDown=0;
            if(Integer.valueOf(type)==0){
                isDown=1;//为下架
            }
            apArticleConfigService.updateByArticleId(Long.valueOf(articleId),isDown);
            return Result.ok("文章或者下架信息成功");
        }
    }
}
